/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package progettode_co.Test;

import com.lowagie.text.DocumentException;
import com.sun.pdfview.PDFFile;
import java.awt.print.*;
import java.io.*;
import java.net.MalformedURLException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xhtmlrenderer.pdf.ITextRenderer;

/**
 *
 * @author Domenico
 */

public class NewClass {
 public static void main(String args[]) {
        try {
            volaSarsizza();
        } catch (MalformedURLException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
 }
    
    private static void volaSarsizza() throws MalformedURLException, FileNotFoundException, DocumentException, IOException {
        //String inputFile = "C:\\fattura.html";
        //String outputFile = "C:\\Fattura.pdf";
        String curDir = System.getProperty("user.dir");
        String inputFile = curDir + "\\Fattura\\fattura.html";
        String outputFile = curDir + "\\Temp\\Fattura.pdf";
        String url = new File(inputFile).toURI().toURL().toString();
        OutputStream os = new FileOutputStream(outputFile);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);
        renderer.layout();
        renderer.createPDF(os); //new PDF is created from the input .xhtml
        os.close();
    }
    private void stampa2() {
        FileInputStream fis = null;
        try
{
// Create a PDFFile from a File reference
File f = new File("c:\\Fattura.pdf");
            fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
PDFPrintPage pages = new PDFPrintPage(pdfFile);
 
// Create Print Job
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();

Paper paper = new Paper();
paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight()); // no margin = no scaling
pf.setPaper(paper);

pjob.setJobName(f.getName());
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
pjob.printDialog(); 
// Send print job to default printer
pjob.print();
    }   catch (PrinterException ex) {
//            Logger.getLogger(FMain.class.getName()).log(Level.SEVERE, null, ex);
        }   catch (IOException ex) {
//            Logger.getLogger(FMain.class.getName()).log(Level.SEVERE, null, ex);
        
 //           Logger.getLogger(FMain.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
  //              Logger.getLogger(FMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
